from rest_framework import serializers

from areas.models import Area

"""
省份
[{'id':xxxx,'name':'xxxx'}]
市  区县
['id':xxx,name:'xxx','subs': [{'id':xxxx,'name':'xxxx'}]]
                                subs是上面这部分
    省份
    10000           北京          None

    市
    10001           北京市         10000

    区县
    10010           昌平区         10001
    10020           海淀区         10001
    10030           朝阳区         10001

"""
class AreaSerializer(serializers.ModelSerializer):

    class Meta:
        # 省份
        # [{'id': xxxx, 'name': 'xxxx'}]
        model = Area
        fields = ['id', 'name']


class SubsAreaSerializer(serializers.ModelSerializer):
    # 市 区县的模型数据 用 AreaSerialzier 进行序列化操作
    subs = AreaSerializer(many=True, read_only=True)

    class Meta:
        model = Area
        fields = ['id', 'name', 'subs']